<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Promise</title>

</head>

<body>

    <script>

        var promise=new Promise(function(resolve,reject){

            // 异步操作写在这里

            resolve("yuan");//成功后的操作

        });

            // reject("shibaile!!:(")//失败 状态

           var p2=new Promise(function(resolve,reject){

               setTimeout(()=>{

                   resolve("2000ms 以后");

               },2000);

          })

       

        Promise.race([promise,p2]).then(function(data){

            //race是两个谁快取谁。如果把rece改all，就是所有异步都完成再处理，且返回结果为数组

            console.log("data from promise:"+data);

            //处理异步操作成功返回的数据

        },function(error){

            console.log("error from promise:"+error);

            //处理异步操作失败返回的数据；

            });

 

    </script>

</body>

</html>